/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.Actions;

import controller.Forms.EnterpriseForm;
import controller.Forms.ResponsibleForm;
import dao.EnterpriseDAO;
import dao.ResponsibleDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Enterprise;
import model.Erro;
import model.Responsible;
import model.User;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Adriano
 */
public class RegisterEnterpriseAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {

            User u = (User) request.getSession().getAttribute("User");

            if (u == null) {
                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");
            } else {
                EnterpriseForm empresaform = (EnterpriseForm) form;
                Enterprise empresa = new Enterprise();
 
                empresa.setTxtCorporateName(empresaform.getTxtCorporateName().toLowerCase());
                empresa.setTxtCnpj(empresaform.getTxtCnpj().toLowerCase().replace(".", "").replace("/", "").replace("-", ""));
                empresa.setTxtStateRegistration(empresaform.getTxtStateRegistration().toLowerCase());
                empresa.setTxtMunicipalRegistration(empresaform.getTxtMunicipalRegistration().toLowerCase());
                empresa.setTxtZipcode(empresaform.getTxtZipCode().toLowerCase().replace("-",""));
                empresa.setTxtAddress(empresaform.getTxtAddress().toLowerCase());
                empresa.setTxtNumero(empresaform.getTxtNumero().toLowerCase());
                empresa.setTxtComplement(empresaform.getTxtComplement().toLowerCase());
                empresa.setTxtDistrict(empresaform.getTxtDistrict().toLowerCase());
                empresa.setTxtCity(empresaform.getTxtCity().toLowerCase());
                empresa.setTxtLstUF(empresaform.getLstUF().toLowerCase());
                empresa.setTxtDdd(empresaform.getTxtDdd().toLowerCase());
                empresa.setTxtPhone(empresaform.getTxtPhone().toLowerCase());
                empresa.setTxtEmail(empresaform.getTxtEmail().toLowerCase());
                empresa.setTxtJointStock(empresaform.getTxtJoinStock().toLowerCase().replace(".","" ).replace(",", "."));
                empresa.setTxtLstStatus(empresaform.getLstStatus().toLowerCase());

                EnterpriseDAO empresaDAO = new EnterpriseDAO();
                empresaDAO.create(empresa);
                empresaDAO.closeDAO();

            //Responsavel
                Responsible responsible = new Responsible();
                responsible.setTxtName(empresaform.getTxtName());
                responsible.setTxtCpf(empresaform.getTxtCpf());
                responsible.setTxtRg(empresaform.getTxtRg());
                responsible.setTxtQualification(empresaform.getTxtQualification());
                responsible.setLstStatusR(empresaform.getLstStatus());

                ResponsibleDAO responsibleDAO = new ResponsibleDAO();

                responsibleDAO.create(responsible);
                responsibleDAO.closeDAO();

                return mapping.findForward("homeAlias");
            }
        } catch (Exception sql) {

            System.out.print("CADASTRAR_EMPRESARESPONSAVELACTION-" + sql.getMessage());
            return mapping.findForward("erroAlias");

        }

    }

}
